
package com.webvihar.access2db.activities;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.webvihar.access2db.R;
import com.webvihar.access2db.model.Session;
import com.webvihar.common.db.DbService;
import com.webvihar.common.utils.Validation;

public class EditSessionActivity extends BaseActivity implements OnClickListener, OnKeyListener  {

	private DbService db;
	private EditText nameTxt;
	private EditText hostTxt;
	private EditText portTxt;
	private EditText usernameTxt;
	private EditText passwordTxt;
	private Button updateSessionBtn;
	private ImageButton addButton;
	private TextView titleTxt;

	private String SESSION_SESSION_NAME = "SessionName";
	private String SESSION_SESSION_HOST = "SessionHost";
	private String SESSION_SESSION_PORT = "SessionPort";
	private String SESSION_SESSION_USERNAME = "SessionUsername";
	private String SESSION_SESSION_PASSWORD = "SessionPassword";

	private String sessionName;
	private String host;
	private String port;
	private String username;
	private String password;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
		setContentView(R.layout.add_session);
		loadBundleData(getIntent().getExtras());
		initUI();
		setTitleBar();
		setListeners();
		initDb();
	}

	private void setTitleBar() {
		titleTxt = (TextView) findViewById(R.id.title_txt);
		titleTxt.setText(getString(R.string.sessions_txt));
		addButton = (ImageButton) findViewById(R.id.title_action_image_btn);
		addButton.setVisibility(View.GONE);
			
	}

	private void loadBundleData(Bundle bundle) {
		sessionName = bundle.getString(SESSION_SESSION_NAME);
		host = bundle.getString(SESSION_SESSION_HOST);
		port = bundle.getString(SESSION_SESSION_PORT);
		username = bundle.getString(SESSION_SESSION_USERNAME);
		password = bundle.getString(SESSION_SESSION_PASSWORD);
	}

	private void initUI(){
		nameTxt = (EditText)findViewById(R.id.add_session_name_txt);
		nameTxt.setText(sessionName);
		hostTxt = (EditText)findViewById(R.id.add_session_host_txt);
		hostTxt.setText(host);
		portTxt = (EditText)findViewById(R.id.add_session_port_txt);
		portTxt.setText(port);
		usernameTxt = (EditText)findViewById(R.id.add_session_username_txt);
		usernameTxt.setText(username);
		passwordTxt = (EditText)findViewById(R.id.add_session_password_txt);
		passwordTxt.setText(password);
		updateSessionBtn = (Button)findViewById(R.id.add_session_add_edit_delete_btn);
		updateSessionBtn.setText(getResources().getString(R.string.edit_session_txt));
		updateSessionBtn.setEnabled(false);
	}

	private void setListeners() {
		updateSessionBtn.setOnClickListener(this);
		nameTxt.setOnKeyListener(this);
		hostTxt.setOnKeyListener(this);
		portTxt.setOnKeyListener(this);
		usernameTxt.setOnKeyListener(this);
		passwordTxt.setOnKeyListener(this);
	}

	private void initDb() {
		db = new DbService(this);
	}

	@Override
	public void onClick(View v) {
		processData();
	}

	private void processData() {
		String name = nameTxt.getText().toString();
		String host = hostTxt.getText().toString();
		String port = portTxt.getText().toString();
		String username = usernameTxt.getText().toString();
		String password = passwordTxt.getText().toString();

		if(isDataValid(name,host,port,username,password)) {
			saveSession(name,host,port,username,password);
			EditSessionActivity.this.finish();
		}
	}

	private void saveSession(String newName, String newHost, String newPort,
			String newUsername, String newPassword) {		
		ObjectContainer dbCon = db.GetDb();
		// create old object
		Session oldSession = new Session(sessionName, host, port, username, password);
		Session newSession = null;
		// get the required object from database
		ObjectSet<Session> sessions = dbCon.queryByExample(oldSession);
		newSession = (Session) sessions.next();
		// update the object
		newSession.sessionName = newName;
		newSession.host = newHost;
		newSession.port	= newPort;
		newSession.username = newUsername;
		newSession.password = newPassword;
		// save the object
		db.Save(newSession);
	}

	private boolean isDataValid(String name, String host, String port,
			String username, String password) {
		if(!Validation.isString(name)) {
			showToast(getResources().getString(R.string.prompt_session_name_txt));
			return false;
		}
		if(!Validation.isHost(host)) {
			showToast(getResources().getString(R.string.prompt_host_txt));
			return false;
		}
		if(!Validation.isPort(port)) {
			showToast(getResources().getString(R.string.prompt_port_txt));
			return false;
		}
		if(!Validation.isUsername(username)) {
			showToast(getResources().getString(R.string.prompt_username_txt));
			return false;
		}
		if(!Validation.isPassword(password)) {
			showToast(getResources().getString(R.string.prompt_password_txt));
			return false;
		}
		return true;
	}

	@Override
	public boolean onKey(View view, int position, KeyEvent keyEvent) {
		if(keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
			return false;
		} else if(view.getId() == R.id.add_session_name_txt || view.getId() == R.id.add_session_host_txt || view.getId() == R.id.add_session_port_txt || view.getId() == R.id.add_session_username_txt || view.getId() == R.id.add_session_password_txt) {
			enableUpdateButton();
		}
		return false;
	}

	private void enableUpdateButton() {
		if(!nameTxt.getText().toString().equalsIgnoreCase(sessionName) || !hostTxt.getText().toString().equalsIgnoreCase(host) || !portTxt.getText().toString().equalsIgnoreCase(port) || !usernameTxt.getText().toString().equalsIgnoreCase(username) || !passwordTxt.getText().toString().equalsIgnoreCase(password)) {
			updateSessionBtn.setEnabled(true);
		} else {
			updateSessionBtn.setEnabled(false);
		}
	}
}